package com.wt.struggle.goodbye.wxutil

import android.app.Activity
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.Drawable
import android.util.Log
import com.tencent.mm.opensdk.modelmsg.*
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.xin.lv.yang.utils.utils.ImageUtil
import com.tencent.connect.share.QQShare
import android.os.Bundle
import com.sina.weibo.sdk.api.WebpageObject
import com.tencent.tauth.IUiListener
import com.tencent.tauth.Tencent
import com.sina.weibo.sdk.api.WeiboMultiMessage
import com.sina.weibo.sdk.share.WbShareHandler
import com.sina.weibo.sdk.utils.Utility
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.Share
import com.wt.struggle.goodbye.info.ShareInfo


/**
 * 分享操作
 */
class ShareUtil {

    companion object {

        var wxApi: IWXAPI? = null

        /**
         * 分享到朋友圈   1 为微信好友    2 为微信朋友圈
         */
        fun share(context: Context, share: ShareInfo, code: Int) {

            Share.saveIsLoginOrShare(context,1)

            wxApi = WXAPIFactory.createWXAPI(context, Contact.WEIXIN_ID, false)
            wxApi!!.registerApp(Contact.WEIXIN_ID)

            Log.i("result", "执行到此信息--------")

            /**
             * 分享类型   1 为网页分享  2 为文字分享  3 为图片分享  4 为视频分享  5 为音乐分享
             */
            val type = share.type

            val scene = when (code) {
                1 -> SendMessageToWX.Req.WXSceneSession
                2 -> SendMessageToWX.Req.WXSceneTimeline
                else -> SendMessageToWX.Req.WXSceneFavorite
            }

            var msg: WXMediaMessage? = null

            when (type) {
                1 -> {
                    val webpage = WXWebpageObject()
                    webpage.webpageUrl = share.url
                    webpage.extInfo = share.content
                    msg = WXMediaMessage(webpage)
                }

                2 -> {
                    val text = WXTextObject()
                    text.text = share.content
                    msg = WXMediaMessage(text)
                }

                3 -> {
                    val image = WXImageObject()
                    image.imagePath = share.imageUrl
                    msg = WXMediaMessage(image)
                }
                4 -> {
                    val video = WXVideoObject()
                    video.videoUrl = share.video
                    msg = WXMediaMessage(video)
                }
                5 -> {
                    val music = WXMusicObject()
                    music.musicUrl = share.music
                    msg = WXMediaMessage(music)
                }
            }

            msg!!.title = share.title
            msg.description = share.content

            Log.i("result", "picUrl========" + share.picUrl)

            if (share.picUrl!=null&&share.picUrl != "") {

                ImageUtil.getInstance().loadByBitmap(context, share.picUrl, 50, 50, object : ImageUtil.OnGetBitmap {
                    override fun getBitmap(bitmap: Bitmap?) {
                        if (bitmap != null) {
                            msg.setThumbImage(bitmap)
                        }

                        val req: SendMessageToWX.Req = SendMessageToWX.Req()
                        req.transaction = System.currentTimeMillis().toString()
                        req.message = msg
                        req.scene = scene

                        wxApi!!.sendReq(req)


                    }

                    override fun getDrawable(drawable: Drawable?) {
                    }

                })

            } else {
                val bitmap = BitmapFactory.decodeResource(context.resources,share.initPic)
                msg.setThumbImage(bitmap)
                val req: SendMessageToWX.Req = SendMessageToWX.Req()
                req.transaction = System.currentTimeMillis().toString()
                req.message = msg
                req.scene = scene

                wxApi!!.sendReq(req)
            }

        }


        /**
         * 分享给qq好友
         */
        fun shareToQQUser(activity: Activity, mTencent: Tencent, content: ShareInfo, back: IUiListener) {
            val params = Bundle()
            params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT)
            params.putString(QQShare.SHARE_TO_QQ_TITLE, content.title)
            params.putString(QQShare.SHARE_TO_QQ_SUMMARY, content.content)
            params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, content.url)
            params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, content.imageUrl)
            params.putString(QQShare.SHARE_TO_QQ_APP_NAME, activity.resources.getString(R.string.app_name))
            mTencent.shareToQQ(activity, params, back)
        }


        /**
         * 分享到QQ空间
         */
        fun shareToQQZone(activity: Activity, mTencent: Tencent, content: ShareInfo, back: IUiListener) {
            val params = Bundle()
            params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN)
            params.putString(QQShare.SHARE_TO_QQ_TITLE, content.title)
            params.putString(QQShare.SHARE_TO_QQ_SUMMARY, content.content)
            params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, content.url)
            params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, content.imageUrl)
            params.putString(QQShare.SHARE_TO_QQ_APP_NAME, activity.resources.getString(R.string.app_name))
            mTencent.shareToQzone(activity, params, back)
        }


        /**
         * 新浪微博分享
         */
        fun shareToSina(context: Context, shareHandler: WbShareHandler, content: ShareInfo) {
            shareHandler.registerApp()

            val mediaObject = WebpageObject()

            mediaObject.identify = Utility.generateGUID()
            mediaObject.title = content.title
            mediaObject.description = content.content

            if (content.imageUrl != "") {
                ImageUtil.getInstance().loadByBitmap(context, content.imageUrl, 50, 50,object :ImageUtil.OnGetBitmap{
                    override fun getBitmap(bitmap: Bitmap?) {
                        mediaObject.setThumbImage(bitmap)
                    }

                    override fun getDrawable(drawable: Drawable?) {
                    }

                })
            } else {
                val bitmap = BitmapFactory.decodeResource(context.resources, R.mipmap.ic_launcher)
                mediaObject.setThumbImage(bitmap)
            }


            mediaObject.actionUrl = content.url
            mediaObject.defaultText = content.title

            val weiboMultiMessage = WeiboMultiMessage()
            weiboMultiMessage.mediaObject = mediaObject

            shareHandler.shareMessage(weiboMultiMessage, false)

        }

    }
}